<template>

<!--  <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">-->
<!--    <el-form-item label="项目ID" prop="projectId">-->
<!--      <el-input-->
<!--          v-model="queryParams.projectId"-->
<!--          placeholder="请输入项目ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--      />-->
<!--    </el-form-item>-->
<!--    <el-form-item label="产品ID" prop="productId">-->
<!--      <el-input-->
<!--          v-model="queryParams.productId"-->
<!--          placeholder="请输入产品ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--      />-->
<!--    </el-form-item>-->
<!--    <el-form-item label="供应商ID" prop="supplierId">-->
<!--      <el-input-->
<!--          v-model="queryParams.supplierId"-->
<!--          placeholder="请输入供应商ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--      />-->
<!--    </el-form-item>-->
<!--    <el-form-item label="关联供应商报价单ID" prop="supplierQuoteId">-->
<!--      <el-input-->
<!--          v-model="queryParams.supplierQuoteId"-->
<!--          placeholder="请输入关联供应商报价单ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--      />-->
<!--    </el-form-item>-->
<!--    <el-form-item label="供应商名称" prop="supplierName">-->
<!--      <el-input-->
<!--          v-model="queryParams.supplierName"-->
<!--          placeholder="请输入供应商名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--      />-->
<!--    </el-form-item>-->
<!--    <el-form-item label="报价日期" prop="quoteDate">-->
<!--      <el-date-picker clearable-->
<!--                      v-model="queryParams.quoteDate"-->
<!--                      type="date"-->
<!--                      value-format="YYYY-MM-DD"-->
<!--                      placeholder="请选择报价日期">-->
<!--      </el-date-picker>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="绘图材质" prop="drawingMaterial">-->
<!--      <el-input-->
<!--          v-model="queryParams.drawingMaterial"-->
<!--          placeholder="请输入绘图材质"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--      />-->
<!--    </el-form-item>-->
<!--    <el-form-item label="材质" prop="material">-->
<!--      <el-input-->
<!--          v-model="queryParams.material"-->
<!--          placeholder="请输入材质"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--      />-->
<!--    </el-form-item>-->
<!--    <el-form-item label="阻燃等级" prop="inflamingRetardingLevel">-->
<!--      <el-input-->
<!--          v-model="queryParams.inflamingRetardingLevel"-->
<!--          placeholder="请输入阻燃等级"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--      />-->
<!--    </el-form-item>-->
<!--    <el-form-item>-->
<!--      <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>-->
<!--      <el-button icon="Refresh" @click="resetQuery">重置</el-button>-->
<!--    </el-form-item>-->
<!--  </el-form>-->

  <el-row :gutter="10" class="mb8">
<!--    <el-col :span="1.5">-->
<!--      <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="Plus"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['scm:scmEstimateQuotes:add']"-->
<!--      >新增</el-button>-->
<!--    </el-col>-->
    <el-col :span="1.5">
      <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scm:scmEstimateQuotes:edit']"
      >修改</el-button>
    </el-col>
    <el-col :span="1.5">
      <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scm:scmEstimateQuotes:remove']"
      >删除</el-button>
    </el-col>
<!--    <el-col :span="1.5">-->
<!--      <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['scm:scmEstimateQuotes:export']"-->
<!--      >导出</el-button>-->
<!--    </el-col>-->
    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :search="false" :columns="columns" showColumnsType="transfer"
                   :linked-id="projectId" :attachment-type="4" :attachment-path="projectCode"></right-toolbar>
  </el-row>

  <el-table v-loading="loading" :data="scmEstimateQuotesList" @selection-change="handleSelectionChange" lazy :scrollbar-always-on="true" :row-class-name="tableRowClassName">
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column label="图号" align="center" prop="scmProjectProducts.drawingNumber" fixed width="98"/>
    <el-table-column label="版本" align="center" prop="scmProjectProducts.version" fixed width="50" v-if="columns[0].visible" />
    <el-table-column label="内版本" align="center" prop="scmProjectProducts.internalVersion" fixed width="65"  v-if="columns[1].visible" />
    <el-table-column label="品名" align="center" prop="scmProjectProducts.productName" fixed  v-if="columns[2].visible" />
    <el-table-column label="供应商名" align="center" prop="supplierName" fixed  v-if="columns[3].visible" />
<!--    <el-table-column label="" align="center" prop="id" />-->
<!--    <el-table-column label="项目ID" align="center" prop="projectId" />-->
<!--    <el-table-column label="产品ID" align="center" prop="productId" />-->
<!--    <el-table-column label="供应商ID" align="center" prop="supplierId" />-->
<!--    <el-table-column label="关联供应商报价单ID" align="center" prop="supplierQuoteId" />-->
    <el-table-column label="报价日期" align="center" prop="quoteDate" width="99"  v-if="columns[4].visible" >
      <template #default="scope">
        <span>{{ parseTime(scope.row.quoteDate, '{y}-{m}-{d}') }}</span>
      </template>
    </el-table-column>
<!--    <el-table-column label="绘图材质" align="center" prop="drawingMaterial"  v-if="columns[5].visible" />-->
    <el-table-column label="材质" align="center" prop="scmProjectProducts.material" v-if="columns[6].visible" />
    <el-table-column label="阻燃等级" align="center" prop="scmProjectProducts.inflamingRetardingLevel"  v-if="columns[7].visible" />
    <el-table-column label="含税" align="center" prop="taxIncluded" width="50" v-if="columns[8].visible" >
      <template #default="scope">
        <dict-tag :options="sys_yes_no" :value="scope.row.taxIncluded"/>
      </template>
    </el-table-column>
    <el-table-column label="含运费" align="center" prop="freightIncluded" width="70" v-if="columns[9].visible" >
      <template #default="scope">
        <dict-tag :options="sys_yes_no" :value="scope.row.freightIncluded"/>
      </template>
    </el-table-column>
    <el-table-column label="一次性" align="center"  v-if="columns[10].visible" >
      <el-table-column label="数量" align="center" prop="disposableQuantity" />
      <el-table-column label="单位" align="center" prop="disposableUnit" />
      <el-table-column label="单价" align="center" prop="disposableUnitPrice" />
    </el-table-column>
    <el-table-column label="试做" align="center"  v-if="columns[11].visible" >
      <el-table-column label="数量" align="center" prop="tryLot" />
      <el-table-column label="单位" align="center" prop="tryUnit" />
      <el-table-column label="单价" align="center" prop="tryUnitPrice" />
      <el-table-column label="总价" align="center" prop="tryTotalPrice" />
    </el-table-column>
    <el-table-column label="量产" align="center"  v-if="columns[12].visible" >
      <el-table-column label="数量" align="center" prop="massLot" />
      <el-table-column label="单位" align="center" prop="massUnit" />
      <el-table-column label="单价" align="center" prop="massUnitPrice" />
    </el-table-column>
    <el-table-column label="模具费" align="center" prop="moldCost"  v-if="columns[13].visible" />
    <el-table-column label="工装费" align="center" prop="jigCost"  v-if="columns[14].visible" />
    <el-table-column label="币种" align="center" prop="supplierCurrency" />
    <el-table-column label="汇率" align="center" prop="supplierCurrencyRate" />
    <el-table-column label="模具试做天数" align="center" prop="moldTryingDeliveryDays"  v-if="columns[15].visible" />
    <el-table-column label="模具使用寿命(次)" align="center" prop="moldLife"  v-if="columns[16].visible" />
    <el-table-column label="一次性交付天数" align="center" prop="disposableDeliveryDays"  v-if="columns[17].visible" />
    <el-table-column label="试做交付天数" align="center" prop="tryDeliveryDays"  v-if="columns[18].visible" />
    <el-table-column label="量产交付天数" align="center" prop="massDeliveryDays"  v-if="columns[19].visible" />
    <el-table-column label="状态" align="center" prop="status"  v-if="columns[20].visible" >
      <template #default="scope">
        <dict-tag :options="scm_select_satus" :value="scope.row.status"/>
      </template>
    </el-table-column>
    <el-table-column label="状态原因" align="center" prop="statusReason"  v-if="columns[21].visible" />
    <el-table-column label="报价文件名字" align="center" prop="quoteFileName"  v-if="columns[22].visible" />
    <el-table-column label="报价文件地址" align="center" prop="quoteFileUrl"  v-if="columns[23].visible" />
    <el-table-column label="沟通内容" align="center" prop="communicationContent" v-if="columns[24].visible"  />
    <el-table-column label="备注" align="left" prop="remarks" :show-overflow-tooltip="true" width="100"  v-if="columns[25].visible" />
    <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280" >
      <template #default="scope">
        <el-button link type="primary" icon="Check" @click="handleAcceptStatus(scope.row)" v-hasPermi="['scm:scmSupplierQuotes:edit']">采纳</el-button>
        <el-button link type="primary" icon="Close" @click="handleDelete(scope.row)" v-hasPermi="['scm:scmSupplierQuotes:remove']">不采纳</el-button>
        <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:scmEstimateQuotes:edit']">修改</el-button>
        <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:scmEstimateQuotes:remove']">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
  />

  <!-- 添加或修改客户报价单对话框 -->
  <el-dialog :title="title" v-model="open" width="1300px" append-to-body>
    <el-form ref="scmEstimateQuotesRef" :model="form" :rules="rules" label-width="120px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="图号" prop="scmProjectProducts.drawingNumber">
            <el-input v-model="form.scmProjectProducts.drawingNumber" placeholder="请输入图号" readonly disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="品名" prop="scmProjectProducts.productName">
            <el-input v-model="form.scmProjectProducts.productName" placeholder="请输入品名" readonly disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="版本" prop="scmProjectProducts.version">
            <el-input v-model="form.scmProjectProducts.version" placeholder="请输入版本" readonly disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="内版本" prop="scmProjectProducts.internalVersion">
            <el-input v-model="form.scmProjectProducts.internalVersion" placeholder="请输入内版本" readonly disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="供应商名称" prop="supplierName">
            <el-input v-model="form.supplierName" placeholder="请输入供应商名称" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="报价日期" prop="quoteDate">
            <el-date-picker clearable
                            v-model="form.quoteDate"
                            type="date"
                            value-format="YYYY-MM-DD"
                            placeholder="请选择报价日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6"></el-col>
        <el-col :span="6">
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
<!--          <el-form-item label="绘图材质" prop="drawingMaterial">-->
<!--            <el-input v-model="form.drawingMaterial" placeholder="请输入绘图材质" />-->
<!--          </el-form-item>-->
        </el-col>
        <el-col :span="6">
          <el-form-item label="材质" prop="material">
            <el-input v-model="form.scmProjectProducts.material" placeholder="请输入材质" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="阻燃等级" prop="inflamingRetardingLevel">
            <el-input v-model="form.scmProjectProducts.inflamingRetardingLevel" placeholder="请输入阻燃等级" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="含税" prop="taxIncluded">
            <el-select v-model="form.taxIncluded" placeholder="请选择是否含税">
              <el-option
                  v-for="dict in sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="(一次性)数量" prop="disposableQuantity">
            <el-input-number v-model="form.disposableQuantity" placeholder="v" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(一次性)单位" prop="disposableUnit">
            <el-input v-model="form.disposableUnit" placeholder="请输入一次性单位" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(一次性)单价" prop="disposableUnitPrice">
            <el-input-number v-model="form.disposableUnitPrice" placeholder="请输入一次性单位单价" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="含运费" prop="freightIncluded">
            <el-select v-model="form.freightIncluded" placeholder="请选择是否含运费">
              <el-option
                  v-for="dict in sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="(试做)数量" prop="tryLot">
            <el-input-number v-model="form.tryLot" placeholder="请输入试做数量" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(试做)单位" prop="tryUnit">
            <el-input v-model="form.tryUnit" placeholder="请输入试做单位" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(试做)单价" prop="tryUnitPrice">
            <el-input-number v-model="form.tryUnitPrice" placeholder="请输入试做单位单价" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(试做)总价" prop="tryTotalPrice">
            <el-input-number v-model="form.tryTotalPrice" placeholder="请输入试做总价" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" :precision="2" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="(量产)数量" prop="massLot">
            <el-input-number v-model="form.massLot" placeholder="请输入量产数量" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(量产)单位" prop="massUnit">
            <el-input v-model="form.massUnit" placeholder="请输入量产单位" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(量产)单价" prop="massUnitPrice">
            <el-input-number v-model="form.massUnitPrice" placeholder="请输入量产单位单价" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="模具费" prop="moldCost">
            <el-input-number v-model="form.moldCost" placeholder="请输入模具费" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工装费" prop="jigCost">
            <el-input-number v-model="form.jigCost" placeholder="请输入工装费" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="币种" prop="supplierCurrency">
            <el-select v-model="form.supplierCurrency" placeholder="请选择供应商币种">
              <el-option
                v-for="dict in scm_currency"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="币种汇率" prop="supplierCurrencyRate">
            <el-input-number v-model="form.supplierCurrencyRate" placeholder="请输入供应商币种汇率" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="模具试做天数" prop="moldTryingDeliveryDays">
            <el-input-number v-model="form.moldTryingDeliveryDays" placeholder="请输入模具试做天数" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="模具使用寿命(次)" prop="moldLife">
            <el-input-number v-model="form.moldLife" placeholder="请输入模具使用寿命(次)" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="一次性交付天数" prop="disposableDeliveryDays">
            <el-input-number v-model="form.disposableDeliveryDays" placeholder="请输入一次性交付天数" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="试做交付天数" prop="tryDeliveryDays">
            <el-input-number v-model="form.tryDeliveryDays" placeholder="请输入试做交付天数" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="量产交付天数" prop="massDeliveryDays">
            <el-input-number v-model="form.massDeliveryDays" placeholder="请输入量产交付天数" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
        </el-col>
        <el-col :span="6">
        </el-col>
        <el-col :span="6">
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option v-for="dict in scm_select_satus" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="报价文件地址" prop="quoteFileUrl">
            <el-input v-model="form.quoteFileUrl" placeholder="请输入报价文件地址" />
          </el-form-item>
        </el-col>
        <el-col :span="6"></el-col>
        <el-col :span="6">
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="状态原因" prop="statusReason">
            <el-input v-model="form.statusReason" type="textarea" placeholder="请输入状态原因" rows="3"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">

        </el-col>
        <el-col :span="6">
          <el-form-item label="沟通内容">
            <el-input v-model="form.communicationContent" type="textarea" rows="3"/>
          </el-form-item>

        </el-col>
        <el-col :span="6">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" rows="3"/>
          </el-form-item>
        </el-col>
      </el-row>

<!--      <el-form-item label="项目ID" prop="projectId">-->
<!--        <el-input v-model="form.projectId" placeholder="请输入项目ID" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="产品ID" prop="productId">-->
<!--        <el-input v-model="form.productId" placeholder="请输入产品ID" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="供应商ID" prop="supplierId">-->
<!--        <el-input v-model="form.supplierId" placeholder="请输入供应商ID" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="关联供应商报价单ID" prop="supplierQuoteId">-->
<!--        <el-input v-model="form.supplierQuoteId" placeholder="请输入关联供应商报价单ID" />-->
<!--      </el-form-item>-->


<!--      <el-form-item label="报价文件名字" prop="quoteFileName">-->
<!--        <el-input v-model="form.quoteFileName" placeholder="请输入报价文件名字" />-->
<!--      </el-form-item>-->


      <el-divider content-position="center">估算报价单额外明细信息</el-divider>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="Plus" @click="handleAddScmEstimateQuoteItems">添加</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="Delete" @click="handleDeleteScmEstimateQuoteItems">删除</el-button>
        </el-col>
      </el-row>
      <el-table :data="scmEstimateQuoteItemsList" :row-class-name="rowScmEstimateQuoteItemsIndex" @selection-change="handleScmEstimateQuoteItemsSelectionChange" ref="scmEstimateQuoteItems">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="序号" align="center" prop="index" width="50"/>
        <el-table-column label="量产数量" prop="massLot" width="150">
          <template #default="scope">
            <el-input v-model="scope.row.massLot" placeholder="请输入量产数量" />
          </template>
        </el-table-column>
        <el-table-column label="量产单位" prop="massUnit" width="150">
          <template #default="scope">
            <el-input v-model="scope.row.massUnit" placeholder="请输入量产单位" />
          </template>
        </el-table-column>
        <el-table-column label="量产单位单价" prop="massUnitPrice" width="150">
          <template #default="scope">
            <el-input v-model="scope.row.massUnitPrice" placeholder="请输入量产单位单价" />
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script setup>

const props = defineProps({
  projectId: {
    type: String,
    default: ''
  },
  projectCode: {
    type: String,
    default: ''
  }
})

// const data = reactive({
//   form: {},
//   queryParams: {
//     projectId: props.projectId,
//   }
// });

import { listScmEstimateQuotes, getScmEstimateQuotes, delScmEstimateQuotes, addScmEstimateQuotes, updateScmEstimateQuotes, updateStatusScmEstimateQuotes } from "@/api/scm/scmEstimateQuotes";

const { proxy } = getCurrentInstance();
const { scm_select_satus, sys_yes_no, scm_currency } = proxy.useDict('scm_select_satus', 'sys_yes_no', 'scm_currency');

const scmEstimateQuotesList = ref([]);
const scmEstimateQuoteItemsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedScmEstimateQuoteItems = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: props.projectId,
    productId: null,
    supplierId: null,
    supplierQuoteId: null,
    supplierName: null,
    quoteDate: null,
    drawingMaterial: null,
    material: null,
    inflamingRetardingLevel: null,
  },
  rules: {
    projectId: [
      { required: true, message: "项目ID不能为空", trigger: "blur" }
    ],
    productId: [
      { required: true, message: "产品ID不能为空", trigger: "blur" }
    ],
    supplierName: [
      { required: true, message: "供应商名称不能为空", trigger: "blur" }
    ],
    quoteDate: [
      { required: true, message: "报价日期不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

// const localProjectId = ref(props.projectId);
// 监听 projectId 变化
watch(
    () => props.projectId,
    (newVal) => {
      console.log('projectId changed to:', newVal);
      queryParams.value.projectId = newVal; // 更新本地变量
      getList();
    }
);

/** 查询客户报价单列表 */
function getList() {
  loading.value = true;
  listScmEstimateQuotes(queryParams.value).then(response => {
    scmEstimateQuotesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

//列表查询
getList();


// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    projectId: null,
    productId: null,
    supplierId: null,
    supplierQuoteId: null,
    supplierName: null,
    quoteDate: null,
    drawingMaterial: null,
    material: null,
    inflamingRetardingLevel: null,
    taxIncluded: null,
    freightIncluded: null,
    disposableQuantity: null,
    disposableUnit: null,
    disposableUnitPrice: null,
    tryLot: null,
    tryUnit: null,
    tryUnitPrice: null,
    tryTotalPrice: null,
    massLot: null,
    massUnit: null,
    massUnitPrice: null,
    moldCost: null,
    jigCost: null,
    supplierCurrency: null,
    supplierCurrencyRate: null,
    moldTryingDeliveryDays: null,
    moldLife: null,
    disposableDeliveryDays: null,
    tryDeliveryDays: null,
    massDeliveryDays: null,
    status: null,
    statusReason: null,
    quoteFileName: null,
    quoteFileUrl: null,
    communicationContent: null,
    remarks: null,
    scmProjectProducts: {
      drawingNumber: null,
      version: null,
      internalVersion: null,
      productName: null,
      material: null,
      inflamingRetardingLevel: null,
      specification: null,
      model: null,
      color: null,
      drawingComposition: null,
    },
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  scmEstimateQuoteItemsList.value = [];
  proxy.resetForm("scmEstimateQuotesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加研讨报价单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getScmEstimateQuotes(_id).then(response => {
    form.value = response.data;
    scmEstimateQuoteItemsList.value = response.data.scmEstimateQuoteItemsList;
    open.value = true;
    title.value = "修改研讨报价单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["scmEstimateQuotesRef"].validate(valid => {
    if (valid) {
      form.value.scmEstimateQuoteItemsList = scmEstimateQuoteItemsList.value;
      if (form.value.id != null) {
        updateScmEstimateQuotes(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addScmEstimateQuotes(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除客户报价单编号为"' + _ids + '"的数据项？').then(function() {
    return delScmEstimateQuotes(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 估算报价单额外明细序号 */
function rowScmEstimateQuoteItemsIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 估算报价单额外明细添加按钮操作 */
function handleAddScmEstimateQuoteItems() {
  let obj = {};
  obj.massLot = "";
  obj.massUnit = "";
  obj.massUnitPrice = "";
  scmEstimateQuoteItemsList.value.push(obj);
}

/** 估算报价单额外明细删除按钮操作 */
function handleDeleteScmEstimateQuoteItems() {
  if (checkedScmEstimateQuoteItems.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的估算报价单额外明细数据");
  } else {
    const scmEstimateQuoteItemss = scmEstimateQuoteItemsList.value;
    const checkedScmEstimateQuoteItemss = checkedScmEstimateQuoteItems.value;
    scmEstimateQuoteItemsList.value = scmEstimateQuoteItemss.filter(function(item) {
      return checkedScmEstimateQuoteItemss.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleScmEstimateQuoteItemsSelectionChange(selection) {
  checkedScmEstimateQuoteItems.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('scm/scmEstimateQuotes/export', {
    ...queryParams.value
  }, `scmEstimateQuotes_${new Date().getTime()}.xlsx`)
}

/** 状态修改按钮操作 */
const dataOfStatus = ref({});
function handleAcceptStatus(row) {
  const _ids = row.id || ids.value;
  dataOfStatus.value.status = 1;
  proxy.$modal.confirm('是否确认采纳供应商报价单编号为"' + _ids + '"的数据项？').then(function() {
    return updateStatusScmEstimateQuotes(_ids, dataOfStatus.value);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("状态修改成功");
  }).catch(() => {});
}

// 添加颜色映射和计数器
const drawingNumberColorMap = new Map();
let colorCounter = 0;
const colorClasses = ['drawing-number-row-0', 'drawing-number-row-1'];

// 修改 tableRowClassName 函数
function tableRowClassName({ row, rowIndex }) {
  if (!row.productId) return '';

  // 检查是否已经为该 drawingNumber 分配了颜色
  if (drawingNumberColorMap.has(row.productId)) {
    return drawingNumberColorMap.get(row.productId);
  }

  // 为新的 drawingNumber 分配颜色
  const colorClass = colorClasses[colorCounter % colorClasses.length];
  drawingNumberColorMap.set(row.productId, colorClass);
  colorCounter++;

  return colorClass;
}

// 列显隐信息
const columns = ref([
  { key: 0, label: `版本`, visible: true },
  { key: 1, label: `内版本`, visible: true },
  { key: 2, label: `品名`, visible: true },
  { key: 3, label: `供应商名`, visible: true },
  { key: 4, label: `报价日期`, visible: true },
  { key: 5, label: `绘图材质`, visible: true },
  { key: 6, label: `材质`, visible: true },
  { key: 7, label: `阻燃等级`, visible: true },
  { key: 8, label: `含税`, visible: true },
  { key: 9, label: `含运费`, visible: true },
  { key: 10, label: `一次性`, visible: true },
  { key: 11, label: `试做`, visible: true },
  { key: 12, label: `量产`, visible: true },
  { key: 13, label: `模具费`, visible: true },
  { key: 14, label: `工装费`, visible: true },
  { key: 15, label: `模具试做天数`, visible: true },
  { key: 16, label: `模具使用寿命`, visible: true },
  { key: 17, label: `一次性交付天数`, visible: true },
  { key: 18, label: `试做交付天数`, visible: true },
  { key: 19, label: `量产交付天数`, visible: true },
  { key: 20, label: `状态`, visible: true },
  { key: 21, label: `状态原因`, visible: true },
  { key: 22, label: `报价文件名字`, visible: true },
  { key: 23, label: `报价文件地址`, visible: true },
  { key: 24, label: `沟通内容`, visible: true },
  { key: 25, label: `备注`, visible: true }

]);



</script>

<style >/* 定义10种不同的背景色 */
.el-table .drawing-number-row-0 {
  background: #e8f5e8;
}

.el-table .drawing-number-row-1 {
  background: #e8e8f5;
}

.el-table .drawing-number-row-2 {
  background: #fff0f0;
}
</style>